package jp.co.ricoh.melody.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import java.text.DateFormatSymbols;

import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.models.entities.AlarmData;
import jp.co.ricoh.melody.views.fragments.CommonDialogFragment;
import jp.co.ricoh.sdk.intent.IntentsRepository;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 */
public class CommonUtils {
    /**
     * Get HDD error from code.
     *
     * @param context Context
     * @param code    HDD error code.
     * @return msg HDD error string.
     */
    public static String getHDDError(Context context, int code) {
        String msg = context.getResources().getString(R.string.hdd_error_msg);
        switch (code) {
            case IntentsRepository.ErrorCode.HDD_NOT_AVAILABLE:
            case IntentsRepository.ErrorCode.LOCKOUT_FAILED:
                msg = context.getResources().getString(R.string.hdd_error_msg);
                break;
            case IntentsRepository.ErrorCode.UNKNOWN_ERROR:
                msg = context.getResources().getString(R.string.hdd_unknown_error);
                break;
            default:
                break;
        }
        return msg;
    }

    /**
     * Show error dialog when the application can not save data to HDD.
     *
     * @param activity The activity.
     * @param code     The error code.
     */
    public static void showHDDErrorDialog(Activity activity, int code) {
        String errorMsg = CommonUtils.getHDDError(activity, code);
        final CommonDialogFragment commonDialogFragment = CommonDialogFragment.newInstance(errorMsg, null, null, activity.getResources().getString(R.string.btn_ok));
        commonDialogFragment.setOnCommonDialogButtonClickListener(new CommonDialogFragment.OnCommonDialogButtonClickListener() {
            @Override
            public void onButtonClicked(CommonDialogFragment.ButtonType type) {
                switch (type) {
                    case RIGHT:
                        commonDialogFragment.dismissAllowingStateLoss();
                        break;
                    default:
                        break;
                }
            }
        });
        commonDialogFragment.show(activity.getFragmentManager(), null);
    }

    /**
     * Format time 24h to 12h.
     *
     * @param alarmData Alarm data.
     * @return
     */
    public static String formatTime(AlarmData alarmData) {
        int intHours = alarmData.getHours();
        if (intHours > ConstantUtils.HOUR_OF_DAY) {
            intHours = intHours % ConstantUtils.HOUR_OF_DAY;
        }
        if (alarmData.getAMPM() == AlarmData.AMPM.AM && intHours == 0) {
            intHours = ConstantUtils.HOUR_OF_DAY;
        }
        String hours = intHours < ConstantUtils.MIN_VALUE_TO_PREFIX ? (ConstantUtils.PREFIX_IF_MINUTES_LESS_THAN_TEN + intHours) : "" + intHours;
        String minutes = alarmData.getMinutes() < ConstantUtils.MIN_VALUE_TO_PREFIX ? ConstantUtils.PREFIX_IF_MINUTES_LESS_THAN_TEN + alarmData.getMinutes() : "" + alarmData.getMinutes();
        return hours
                + ConstantUtils.SEPARATOR_BETWEEN_HOUR_AND_MINUTE
                + minutes;
    }

    /**
     * Format AM/PM to English or Japanese.
     *
     * @param context   The context.
     * @param alarmData Alarm data.
     * @return
     */
    public static String formatAMPM(Context context, AlarmData alarmData) {
        String ampm = alarmData.getAMPM().toString();
        if (TextUtils.equals(ampm, AlarmData.AMPM.AM.toString())) {
            ampm = context.getResources().getString(R.string.am);
        } else if (TextUtils.equals(ampm, AlarmData.AMPM.PM.toString())) {
            ampm = context.getResources().getString(R.string.pm);
        }
        return ConstantUtils.SPACE_STRING + ampm + ConstantUtils.SPACE_STRING;
    }

    /**
     * Get day in text from the position of day in week
     *
     * @param day
     * @return
     */
    public static String getReadableDay(int day) {
        DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
        return dateFormatSymbols.getShortWeekdays()[day + ConstantUtils.OFFSET_DAY_OF_WEEK].toUpperCase();
    }
}
